package com.supermap.dataservice.cloud.science.assets;

import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.assets.FlieDocService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2019年10月28 17:52
 * @email 1301584033@qq.com
 */
@Service
public class FlieDocServiceImpl implements FlieDocService {
    @Resource
    FlieDocMapper flieDocMapper;
    String date= DateUtil.now();
    @Override
    public JSONUtils mapGetArea(String areaCode) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(flieDocMapper.mapGetArea(areaCode));
    }

    @Override
    public JSONUtils getFileDoc(String areaCode, Integer page, Integer size) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData( flieDocMapper.getFileDoc(areaCode,(page-1)*size,size)).setCountSum(flieDocMapper.getFileDoc1(areaCode,(page-1)*size,size));
    }

    @Override
    public JSONUtils insert(String fileName, String title, String userId,String areaCode) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        flieDocMapper.insert(fileName,title,date,userId,areaCode);
        return new JSONUtils();
    }

    @Override
    public JSONUtils delFileDoc(Integer id, String userId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String roleCode=flieDocMapper.getUserRole(userId);
        if(roleCode.equals("NQYWRY")||roleCode.equals("admin")){
            flieDocMapper.delFileDoc(id,userId,date);
            return new JSONUtils();
        }
        List<Map<String,Object>> list= flieDocMapper.getObject(id,userId);
        if(list.size()>0){
            flieDocMapper.delFileDoc(id,userId,date);
            return new JSONUtils();
        }

        return new JSONUtils().setCode(100).setMsg("你没有权限删除！");
    }
}
